import { LVector3 } from "../../Moduls/Tools/LMath";
import GameHandler, { BufType, LayerIndex } from "../GameHandler";

enum EnemyState{
    idle,
    chase,
    attack,
    layoff,
    winner,
}

export default class EnemayAction extends Laya.Script {
    public DetecTarget: Laya.Sprite3D;
    private self:Laya.Sprite3D;
    private state:EnemyState;

    private m_Ray: Laya.Ray;
    private m_OutHitInfo: Laya.HitResult;
    public speed: number = 0.03;
    public m_AttackDistance:number;

    constructor() {
        super();
        this.m_Ray = new Laya.Ray(LVector3.zero, new Laya.Vector3(0, -2, 0));
        this.m_OutHitInfo = new Laya.HitResult();
        this.m_AttackDistance = 0.5;
    }

    onAwake() {
        this.self = this.owner as Laya.Sprite3D;
        this.state = EnemyState.idle;
    }

    onStart() {
    }

    onEnable(){
        this.RegistEvent();
    }
    onDisable(){
        this.UnRegistEvent();
    }

    private RegistEvent(){
        Laya.stage.on("Dest1Arrived",this, this.OnTaskFailed);
    }
    private UnRegistEvent(){
        Laya.stage.off("Dest1Arrived",this, this.OnTaskFailed);
    }
    private OnTaskFailed(){
        this.state = EnemyState.layoff;
    }

    onUpdate() {
        switch(this.state){
            case EnemyState.idle:
                {
                    if( Math.abs(this.self.transform.position.x-GameHandler.Player.transform.position.x) < GameHandler.TrigEnemyChaseDistance &&
                    Math.abs(this.self.transform.position.x-GameHandler.Player.transform.position.x) < GameHandler.TrigEnemyChaseDistance)
                    {
                        if(Laya.Vector3.distance(this.self.transform.position,GameHandler.Player.transform.position) < GameHandler.TrigEnemyChaseDistance){
                            this.state = EnemyState.chase;
                        }
                    }
                }break;
            case EnemyState.chase:
                {
                    this.onChaseUpdate();
                    if( Math.abs(this.self.transform.position.x-GameHandler.Player.transform.position.x) < this.m_AttackDistance &&
                    Math.abs(this.self.transform.position.x-GameHandler.Player.transform.position.x) < this.m_AttackDistance)
                    {
                        if(Laya.Vector3.distance(this.self.transform.position,GameHandler.Player.transform.position) < this.m_AttackDistance){
                            this.state = EnemyState.attack;
                        }
                    }
                }break;
            case EnemyState.attack:
                {
                    Laya.stage.event("EnemyAttacked", this.self);
                    this.state = EnemyState.winner;
                }break;
            case EnemyState.layoff:
                {

                }break;
            case EnemyState.winner:
                {

                }break;
        }
    }
    onChaseUpdate(){
        let targetpos = GameHandler.Player.transform.position.clone();
        targetpos.y = this.self.transform.position.y;
        let rot = this.self.transform.lookAt(targetpos,LVector3.up);
        let direct = LVector3.Subtract(targetpos,this.self.transform.position);
        direct = LVector3.Normalize(direct);

        let speedX: number = direct.x * this.speed;
        let speedZ: number = direct.z * this.speed;

        let rayOrigin: Laya.Vector3 = LVector3.Add(this.self.transform.position, new Laya.Vector3(speedX, 2, speedZ));
        this.m_Ray.origin = rayOrigin; 
        if( GameHandler.GameScene.physicsSimulation.rayCast(this.m_Ray,this.m_OutHitInfo) ){
            let hittarget:Laya.Sprite3D = this.m_OutHitInfo.collider.owner as Laya.Sprite3D;
            if(hittarget.layer != LayerIndex.Brick && hittarget.layer != LayerIndex.Ground){
                speedX = speedZ = 0;
            }
        }
        else{
            speedX = speedZ = 0;
        }
        this.self.transform.translate(new Laya.Vector3(speedX, 0, speedZ), false);
    }
}